<template>
  <div>
    <!-- card:展示三级联动 -->
    <el-card>
      <Category @getCategoryId="getCategoryId" />
    </el-card>
    <el-card style="margin: 20px 0">
      <!-- 场景0 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 展示SPU(类的地方) -->
        <el-table
          style="width: 100%; margin: 10px 0"
          border
          :data="records"
          v-loading="loading"
          element-loading-text="玩命加载中..."
          element-loading-spinner="el-icon-loading"
        >
          <el-table-column label="序号" width="80" type="index" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="success"
                size="mini"
                title="添加SKU"
                icon="el-icon-plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                size="mini"
                title="修改SKU"
                icon="el-icon-edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="mini"
                title="查看SUP的全部的SKU"
                icon="el-icon-info"
                @click="lookSku(row)"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                title="删除SPU"
                @click="deleteSpu(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 
            分页器
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
          :pager-count="9"  设置连续页码数
         -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          prev-text="上一页"
          next-text="下一页"
          layout="prev, pager, next, jumper,->, sizes,total "
          :total="total"
          :pager-count="9"
          background
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScene="changeScene"
      ></SkuForm>
    </el-card>
    <!-- 对话框 -->
    <el-dialog title="SKU列表" :visible.sync="dialogTableVisible">
      <el-table
        :data="skuList"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column
          property="skuName"
          label="名称"
          width="250"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="100"
        ></el-table-column>
        <el-table-column property="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  components: {
    SpuForm,
    SkuForm,
  },
  name: "Spu",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1, //分页器的当前页码
      limit: 3, //当前一页展示几条数据
      total: 0, //一共的数据
      records: [], //展示数据
      loading: false, //loading 加载属性
      scene: 0, //  切换场景属性
      dialogTableVisible: false, //控制对话框隐藏
      skuList: [], //存储sku的照片墙
      loading: false, //加载效果
    };
  },
  methods: {
    getCategoryId({ id, flag }) {
      switch (flag) {
        case 1:
          this.category1Id = id;
          this.category2Id = "";
          this.category3Id = "";
          break;
        case 2:
          this.category2Id = id;
          this.category3Id = "";
          break;
        case 3:
          this.category3Id = id;
          //   发请求-获取Spu列表的数据进行展示
          // 发请求之前loading开始转圈圈
          this.getSpuList();
          this.loading = true;
          break;
      }
    },
    // 获取SPU(类的)实例进行展示
    async getSpuList(pager = 1) {
      this.loading = true;
      this.page = pager;
      // 先整理参数 page limit category3Id
      let { page, limit, category3Id } = this;
      let result = await this.$ajax.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        // 存储数据
        this.total = result.data.total;
        this.records = result.data.records;
        this.loading = false;
      }
    },
    // 当pagesize发生变化的时候触发
    handleSizeChange(limit) {
      // 整理参数
      this.limit = limit;
      this.getSpuList();
    },
    // 添加Spu的回调
    addSpu() {
      this.scene = 1;
      this.$refs.spu.initAddSpuData(this.category3Id);
    },
    // 修改spu的回调
    updateSpu(row) {
      this.scene = 1;
      // 通过ref实现父组件获取子组件(子组件的数据+方法)
      this.$refs.spu.initData(row.id);
    },
    // 子组件绑定的自定义事件回调
    changeScene({ scene, flag }) {
      // 修改场景
      this.scene = scene;
      // 获取最新的数据
      // 当vc切换场景的时候:如果修改,留在当前页,如果添加留在第一个
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    // 切换场景||添加SUP实例SKU
    addSku(row) {
      this.scene = 2;
      // 父组件获取子组件SkuForm,调用子组件的方法
      // 整理子组件需要的参数[发请求所使用的的]
      const { category1Id, category2Id } = this;
      this.$refs.sku.initData(category1Id, category2Id, row);
    },
    // 删除SUp的方法
    async deleteSpu(row) {
      try {
        // 删除的请求
        await this.$ajax.spu.reqDeleteSpu(row.id);
        // 获取最新的SPu数据进行展示
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
        // 提示信息
        this.$message({ type: "success", message: "删除成功" });
      } catch (error) {
        this.$message({ type: "error", message: "删除失败" });
      }
    },
    // 查看sku
    async lookSku(spu) {
      this.dialogTableVisible = true;
      // loading效果开始加载
      this.loading = true;
      let result = await this.$ajax.spu.reqSearchSku(spu.id);
      console.log(result);
      if (result.code == 200) {
        this.skuList = result.data;
        // loading效果停止转动
        this.loading = false;
      } else {
        this.$message({
          type: "error",
          message: "获取数据失败",
        });
      }
    },
  },
};
</script>

<style>
</style>